package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    IRoleService roleService;

    @RequestMapping("view")
    public String view(){
        return "role";
    }


    @RequestMapping("query")
    @ResponseBody
    public PageResult query(QueryObject qo){
        return roleService.query(qo);
    }


    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Role role){
        try {
            if(role.getId()!=null){
                roleService.updateByPrimaryKey(role);
            }else{
                roleService.insert(role);
            }
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult().mark("保存失败!");
        }

    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Role> selectAll(){
        return roleService.selectAll();
    }


    @RequestMapping("selectByEmployeeId")
    @ResponseBody
    public List<Long> selectByEmployeeId(Long employeeId){
        return roleService.selectByEmployeeId(employeeId);
    }



}

